package com.foreknow.web;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.User;
import com.foreknow.service.UserServicer;
import com.foreknow.utils.Constance;
import com.foreknow.utils.TokenUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController{
    @Autowired
    private UserServicer userServicer;
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public String login(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        String username=(String)para.get("username");
        String password=(String)para.get("password");
//        User user = new User(username,password);
//        String token= TokenUtil.sign(user);
//        user.setToken(token);
        User user = userServicer.Login(username, password);
        HashMap<String,Object> hs=new HashMap<>();
//    hs.put("token",token);
        hs.put("data",user);
        if (user!=null){
            hs.put("meta",new Constance("msg",200));
        }else {
            hs.put("meta",new Constance("msg",403));
        }

        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value="/findUserById",method = RequestMethod.GET)
    public String findUserById(@RequestParam Integer id) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        User user = userServicer.queryUserById(id);
        if(user!=null){
            hs.put("meta", new Constance("添加成功", 200));
        }else{
            hs.put("meta", new Constance("添加失败", 401));
        }
        hs.put("data", user);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/test",method = RequestMethod.POST)
    @ResponseBody
    public String test(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("data","data");
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/findUserByPage",method = RequestMethod.GET)
    public String findUserByPage(@RequestParam Map<String,Object> para)throws JsonProcessingException {
        //获取参数
        String param = (String)para.get("param");
        Integer pageNum =Integer.parseInt((String) para.get("pageNum"));
        Integer pageSize=Integer.parseInt((String) para.get("pageSize"));
        HashMap<String,Object> hs=new HashMap<>();
        //设置分页信息
        PageHelper.startPage(pageNum,pageSize);
        List<User> userList = userServicer.queryAllUser(param);
        PageInfo<User> info = new PageInfo<>(userList);
        //给map添加数据
        hs.put("totalpage",info.getTotal());
        hs.put("pagenum",info.getPageNum());
        hs.put("users",userList);
        if (userList.size()>0){//如果获取到数据
            hs.put("meta",new Constance("获取成功",200));
        }else{
            hs.put("meta",new Constance("获取失败",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteUserById",method = RequestMethod.GET)
    public String deleteUserById(@RequestBody Map<String,Object> para)throws JsonProcessingException {
        Integer id =Integer.parseInt((String) para.get("id"));
        HashMap<String,Object> hs=new HashMap<>();
        int i = userServicer.deleteById(id);
        hs.put("data",i);
        if (i>0){
            hs.put("meta",new Constance("删除用户成功",200));
        }else {
            hs.put("meta",new Constance("删除用户失败",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/deleteUsersById", method = RequestMethod.POST)
    public String deleteUsers(@RequestBody Map<String,List<Integer>>ids) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        Integer num = userServicer.removeUsers(ids.get("ids"));
        if(num>0){
            hs.put("meta", new Constance("删除成功！"+num+"条数据被删除", 200));
        }else{
            hs.put("meta", new Constance("删除失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/insertMessage",method = RequestMethod.GET)
    public String insertUserMessage(@RequestBody Map<String,Object> para)throws JsonProcessingException {
        String username = (String)para.get("username");
        String password = (String)para.get("password");
        String realName = (String)para.get("realName");
        Integer useType =Integer.parseInt((String) para.get("userType"));
        Integer docTitleId =Integer.parseInt((String) para.get("docTitleId"));
        String isScheduling = (String)para.get("isScheduling");
        Integer deptId =Integer.parseInt((String) para.get("deptId"));
        Integer registLeId =Integer.parseInt((String) para.get("registLeId"));
        Integer DelMark =Integer.parseInt((String) para.get("DelMark"));
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setRealname(realName);
        user.setUsetype(useType);
        user.setDoctitleid(docTitleId);
        user.setIsscheduling(isScheduling);
        user.setDeptid(deptId);
        user.setRegistleid(registLeId);
        user.setDelmark(DelMark);
        int i = userServicer.insertUserMessage(user);
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("data",i);
        if (i>0){
            hs.put("meta",new Constance("添加用户成功",200));
        }else {
            hs.put("meta",new Constance("添加用户失败",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/modifyUser", method = RequestMethod.GET)
    public String modifyUser(@RequestBody User user) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        if(userServicer.modifyUser(user)){
            hs.put("meta", new Constance("修改成功", 200));
        }else{
            hs.put("meta", new Constance("修改失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}